package com.luck.emp.saleman.logic;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;

import com.luck.emp.entity.EmpTSalesman;
import com.luck.emp.saleman.qry.SalemanQry;
import com.luck.pf.common.entity.UserBean;
import com.luck.pf.framework.dao.BaseJdbcDao;
import com.luck.pf.framework.logic.BaseLogic;
import com.luck.pf.util.CriteriaUtil;
import com.luck.pf.util.DateUtil;
import com.luck.pf.util.Page;

/**
 * 说明 Copyright: Copyright (c) 2011 深圳**科技
 * 
 * @author
 * 
 */
public class SalemanLogic extends BaseLogic implements ISalemanLogic {
	private BaseJdbcDao jdbcDao;

	/**
	 * 查找分页信息
	 */
	public Page findSalemanPage(SalemanQry qry) {
		Criteria criteria = baseDao.createCriteria(EmpTSalesman.class);
		CriteriaUtil.addFullLike(criteria, "name", qry.getName());
		CriteriaUtil.addFullLike(criteria, "tel", qry.getTel());
		CriteriaUtil.addEq(criteria, "status", qry.getStatus());
		//CriteriaUtil.addEq(criteria, "deptId", qry.getDeptId());
		CriteriaUtil.addFullLike(criteria, "saleCode", qry.getSaleCode());
		Order order = Order.desc("id");
		criteria.addOrder(order);
		if(qry.getOrgId() != null)
			CriteriaUtil.addEq(criteria, "deptId", qry.getOrgId());
		else{
			criteria.createAlias("dept", "d");
			filterData(criteria, qry.getUserInfo(), "d", null);
		}
		return baseDao.pagedQuery(criteria, qry.getPageNo(), qry.getPageSize());
	}
	private EmpTSalesman findSaleman(Long deptId, String saleCode){
		List<EmpTSalesman> ls = super.baseDao.find(" FROM EmpTSalesman WHERE saleCode=? and deptId=?",new Object[]{saleCode.trim(), deptId});
		if(ls !=null && !ls.isEmpty())
			return ls.get(0);
		return null;
	}
	/**
	 * 保存
	 **/
	public String saveSaleman(UserBean userBean, EmpTSalesman entity) throws Exception{
		StringBuffer bf = new StringBuffer();
		if(entity.getId() ==null){//新增
			if(findSaleman(userBean.getDeptId(), entity.getSaleCode()) !=null)
				bf.append("<br>此业务员代码已经存在;");
			entity.setCreateDate(DateUtil.getDate());
			entity.setCreateUser(userBean.getUser());
			entity.setModiDate(DateUtil.getDate());
			entity.setModiUser(userBean.getUser());
			entity.setDeptId(userBean.getOrgId());
		}
		entity.setModiDate(DateUtil.getDate());
		entity.setModiUser(userBean.getUser());

		// 如果保存逻辑不通过则会在BF中增加提示信息，如不为空，则抛出错误提示。
		if (StringUtils.isBlank(bf.toString())) {
			super.save(entity);
		} else
			throw new Exception(bf.toString());
		return bf.toString();
	}

	/**
	 * 删除
	 */
	public String deleteSaleman(UserBean userBean, EmpTSalesman entity) throws Exception{
		StringBuffer bf = new StringBuffer();
		int count = super.baseDao.count("  select count(*) from EmpTSlOrderM t where salemanId = ?",entity.getId());
		if (count > 0)
			bf.append("<br>此业务员已经有销售业务数据");
		int count2 = super.baseDao.count("  select count(*) from EmpTWhOrderM t where salemanId = ?",entity.getId());
		if (count2 > 0)
			bf.append("<br>此业务员已经有出入库订单数据");

		entity = super.get(EmpTSalesman.class, entity.getId());
		if(!entity.getDeptId().equals(userBean.getDeptId()))
			bf.append("<br>不属于您所在的机构");
		
		if (StringUtils.isBlank(bf.toString())) {
			super.delete(entity);
		} else
			throw new Exception(bf.toString());
		return bf.toString();
	}

	public BaseJdbcDao getJdbcDao() {
		return jdbcDao;
	}

	public void setJdbcDao(BaseJdbcDao jdbcDao) {
		this.jdbcDao = jdbcDao;
	}

}